package com.youdelu.basicframe.base;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.youdelu.basicframe.R;
import com.youdelu.basicframe.utils.DisplayUtil;
import com.youdelu.basicframe.utils.StringUtil;
import com.youdelu.basicframe.widget.PopDialog;

/**
 * Create by 游德禄 on 2019-9-21
 *
 * 对话框
 *
 */
public abstract class BaseDialog extends PopDialog {

    private ViewFlipper v;
    private ScrollView textBox;
    private TextView title,text,submitTxt,cancelTxt;
    private ImageView close;
    private View titleLine;
    private LinearLayout box,body,submit,cancel,head;
    public boolean isShow = false ;
    public  View view ;
    private String id;
    private String tempData;
    private Object tempObject;

    public Object getTempObject() {
        return tempObject;
    }

    public void setTempObject(Object tempObject) {
        this.tempObject = tempObject;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTempData() {
        return tempData;
    }

    public void setTempData(String tempData) {
        this.tempData = tempData;
    }

    public BaseDialog(Context context) {
        this(context,-1);
    }
    public BaseDialog(Context context,int r) {
        this(context,r,true);
    }
    public BaseDialog(Context context, int r, boolean marchParent) {
        super(context, R.layout.dialog_base,marchParent);
        setOutsideTouchable(true);
        view = getView();
        if(view!=null) {
            v = view.findViewById(R.id.content);
            if (r != -1) {
                View child = LayoutInflater.from(context).inflate(r, null);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                v.addView(child, lp);
            } else {
                v.setVisibility(View.GONE);
            }
            title = view.findViewById(R.id.title);
            titleLine = view.findViewById(R.id.titleLine);
           // NLog.e("DateSeletDialog","onCreate3",(titleLine==null));
            text = view.findViewById(R.id.text);
            textBox = view.findViewById(R.id.textBox);
            close = view.findViewById(R.id.close);
            submit = view.findViewById(R.id.submit);
            cancel = view.findViewById(R.id.cancel);
            box = view.findViewById(R.id.box);
            body = view.findViewById(R.id.body);
            head = view.findViewById(R.id.head);
            submitTxt=view.findViewById(R.id.submitTxt);
            cancelTxt=view.findViewById(R.id.cancelTxt);
            close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                    onClose();
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onCancel()) {
                        if(onResult!=null){
                            onResult.onNo();
                        }
                        dismiss();
                    }
                }
            });
            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onSubmit()) {
                        if(onResult!=null){
                            onResult.onYes();
                        }
                        dismiss();
                    }
                }
            });
            super.setOnDissmissListener(new OnDismiss() {
                @Override
                public void dismiss() {
                    isShow = false;
                    onClose();
                }
            });
        }
    }


    /**
     * 设置两边离屏幕边缘的距离
     * @param pd
     */
    public com.youdelu.basicframe.base.BaseDialog setPaddingLR(int pd){
        if(pd>0){
             box.setPadding(pd,0,pd,0);
        }
        return this;
    }

    /**
     * 设置背景
     * @param R
     */
    public com.youdelu.basicframe.base.BaseDialog setBg(int R){
        body.setBackgroundResource(R);
        return this;
    }

    /**
     * 设置标题
     * @param title
     */
    public com.youdelu.basicframe.base.BaseDialog setTitle(String title){
        if(StringUtil.isEmpty(title)){
            titleLine.setVisibility(View.GONE);
        }else{
            titleLine.setVisibility(View.VISIBLE);
        }
        this.title.setText(title);
        return this;
    }

    public com.youdelu.basicframe.base.BaseDialog setSubmitTxt(String txt){
        this.submitTxt.setText(txt);
        return this;
    }

    public com.youdelu.basicframe.base.BaseDialog setCancelTxt(String txt){
        this.cancelTxt.setText(txt);
        return this;
    }

    /**
     * 设置内容
     * @param text
     */
    public com.youdelu.basicframe.base.BaseDialog setContent(String text){
        if(this.text.getVisibility()==View.GONE) {
            this.textBox.setVisibility(View.VISIBLE);
            this.text.setVisibility(View.VISIBLE);
        }
        this.text.setText(text);
        return this;
    }

    public void setContentGravity(int gravity){
        text.setGravity(gravity);
    }
    public String getText(){
        return text.getText().toString();
    }

    /**
     * 隐藏标题
     */
    public com.youdelu.basicframe.base.BaseDialog hideHeader(){
        this.head.setVisibility(View.GONE);
        this.titleLine.setVisibility(View.GONE);
        return this;
    }

    public com.youdelu.basicframe.base.BaseDialog showHeader(){
        this.head.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 隐藏提交按钮
     */
    public com.youdelu.basicframe.base.BaseDialog hideSubmit(){
        this.submit.setVisibility(View.GONE);
        return this;
    }

    /**
     * 隐藏取消按钮
     */
    public com.youdelu.basicframe.base.BaseDialog hideCancl(){
        this.cancel.setVisibility(View.GONE);
        return this;
    }

    public void hidden(){
        box.setVisibility(View.GONE);
    }

    public void visibility(){
        box.setVisibility(View.VISIBLE);
    }


    /**
     * 显示
     * @return
     */
    public void show(){
          show(0,Gravity.BOTTOM);
    }
    /**
     * 显示
     * @param position
     * @param gravity
     */
    public void show(int position,int gravity){
        this.isShow = true;
        if(gravity==Gravity.BOTTOM){
            setBg(R.drawable.popwin_sharp);
        }else{
            setBg(R.drawable.dialog_sharp);
            setPaddingLR(DisplayUtil.dip2px(context,16));
        }
        super.show(position,gravity);
        //return this;
    }

    public void dismiss(){
        onClose();
        super.dismiss();
    }

    public abstract void onClose();
    public abstract boolean onCancel();
    public abstract boolean onSubmit();
    private OnResult onResult;

    public OnResult getOnResult() {
        return onResult;
    }

    public void setOnResult(OnResult onResult) {
        this.onResult = onResult;
    }

    public interface OnResult{
        void onYes();
        void onNo();
    }
}
